package com.lmpop.createIndex.search;

import com.lmpop.createIndex.searchModel.Queue;
import com.lmpop.createIndex.searchModel.QueueValue;
import com.lmpop.createIndex.searchModel.SearchData;
import com.lmpop.createIndex.searchModel.SearchModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

public class CategoryIndexFiles
{
  public ProperUtil properUtil;
  private String file_path;

  public CategoryIndexFiles()
  {
    this.properUtil = new ProperUtil();
    this.file_path = this.properUtil.getFilePathByModel("category");
  }

  public String getCategorySelect(String cat_id_para, String class_type) throws IOException {
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();
    Queue queue = new Queue(new QueueValue[] { new QueueValue("sort_no", 2, true) });
    paraList.add(new SearchModel("0", "cat_level", "1"));
    paraList.add(new SearchModel("0", "class_type", class_type));
    ArrayList list = search.getSearchInfo(this.file_path, paraList, queue);
    StringBuffer catSelect = new StringBuffer();
    if ((list != null) && (list.size() > 0)) {
      Hashtable map = new Hashtable();
      for (int i = 0; i < list.size(); i++) {
        map = (Hashtable)list.get(i);
        String cat_id = ""; String cat_name = "";
        if (map.get("cat_id") != null) cat_id = map.get("cat_id").toString();
        if (map.get("cat_name") != null) cat_name = map.get("cat_name").toString();
        String select = "";
        if (cat_id.equals(cat_id_para)) {
          select = "selected";
        }
        catSelect.append("<option value='" + cat_id + "' " + select + ">" + cat_name + "</option>");
      }
    }
    return catSelect.toString();
  }

  public ArrayList getCategoryByUpCatId(String up_cat_id, String class_type) throws IOException {
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();
    if (up_cat_id.equals(""))
      paraList.add(new SearchModel("0", "cat_level", "1"));
    else {
      paraList.add(new SearchModel("0", "up_cat_id", up_cat_id));
    }
    paraList.add(new SearchModel("0", "class_type", class_type));
    return search.getSearchInfo(this.file_path, paraList);
  }

  public String getCatNameById(String cat_id) throws IOException {
    if (cat_id.equals("")) return "";
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "cat_id", cat_id));
    ArrayList list = search.getSearchInfo(this.file_path, paraList);
    String cat_name = "";
    if ((list != null) && (list.size() > 0)) {
      Hashtable map = (Hashtable)list.get(0);
      if (map.get("cat_name") != null) cat_name = map.get("cat_name").toString();
    }
    return cat_name;
  }

  public ArrayList getCategoryByUpLevel(String cat_level, String class_type) throws IOException {
    SearchData search = new SearchData();

    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "cat_level", cat_level));
    paraList.add(new SearchModel("0", "class_type", class_type));
    return search.getSearchInfo(this.file_path, paraList);
  }

  public ArrayList getClassInfoList(String class_type, String up_cat_id)
    throws IOException
  {
    SearchData search = new SearchData();

    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "class_type", class_type));
    paraList.add(new SearchModel("0", "up_cat_id", up_cat_id));
    Queue queue = new Queue(new QueueValue[] { new QueueValue("sort_no", 2, true) });

    ArrayList classList = search.getSearchInfo(this.file_path, paraList, queue);

    return classList;
  }

  public String getClassIdByName(String cat_name, String class_type, String up_cat_id) throws IOException
  {
    SearchData search = new SearchData();
    String class_id_val = "";
    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "cat_name", cat_name));
    paraList.add(new SearchModel("0", "class_type", class_type));
    paraList.add(new SearchModel("0", "up_cat_id", up_cat_id));

    ArrayList classList = search.getSearchInfo(this.file_path, paraList);

    if ((classList != null) && (classList.size() > 0)) {
      for (int i = 0; i < classList.size(); i++)
      {
        Hashtable map = (Hashtable)classList.get(i);
        class_id_val = map.get("cat_id").toString();
      }
    }
    return class_id_val;
  }
}